﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BuXiaChu.OrderDishes.BLL.Utility;
using System.IO;
using BuXiaChu.OrderDishes.BLL.SiteConfig;

namespace BuXiaChu.OrderDishes.BLL.FileStore
{
    public abstract class FileUploadProviderBase : IFileUploadProvider
    {
        #region protected

        protected FileServerMap _FileServerMap = null;
        protected FileServer _FileServer = null;
        protected UploadFileEntity _UploadFileEntity = null;
        protected string _ServiceType = null;
        protected string _MimeItemKey = "image";

        #endregion

        protected virtual void MatchFileServer()
        {
            #region

            _FileServerMap = SiteConfigManager.Intance.FileServerMapList.GetFileServerMap(_UploadFileEntity.UploadDate, _ServiceType);
            if (_FileServerMap == null)
            {
                throw new Exception("查询不到文件服务器匹配节点");
            }
            else
            {
                _FileServer = SiteConfigManager.Intance.FileServerList.GetFileServer(_FileServerMap.MapName);
                if (_FileServer == null)
                {
                    throw new Exception("根据节点‘" + _FileServerMap.MapName + "’查询不到服务器");
                }
            }
            #endregion
        }

        protected virtual void ValidateFileMime()
        {
            #region
            var extName = Path.GetExtension(_UploadFileEntity.ClientFileName).ToLower();
            var flag = false;
            MimeItem mimeItem = _FileServer.MimeItemList.GetMimeItem(_MimeItemKey);
            if (mimeItem != null)
            {
                flag = mimeItem.MimeTypeList.Split(';').ToList<string>().Contains(extName);
                if (flag == false)
                {
                    throw new Exception("上传的文件扩展名未知" + extName);
                }
            }
            #endregion
        }

        public virtual void Save()
        {
            #region
            if (_UploadFileEntity.Content.Length <= 0)
                return;
            using (ImpersonateUser iu = new ImpersonateUser())
            {
                ImpersonateUser.ValidUser(iu, _FileServer.Indentity);

                string dirPath = Path.GetDirectoryName(_UploadFileEntity.ServerStorePath);
                if (!System.IO.Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                }

                using (FileStream writer = File.Open(_UploadFileEntity.ServerStorePath, FileMode.OpenOrCreate))
                {
                    writer.Write(_UploadFileEntity.Content, 0, _UploadFileEntity.Content.Length);
                    writer.Flush();
                    writer.Close();
                }
            }
            #endregion
        }

        public virtual void Delete()
        {
            #region
            using (ImpersonateUser iu = new ImpersonateUser())
            {
                ImpersonateUser.ValidUser(iu, _FileServer.Indentity);

                if (File.Exists(_UploadFileEntity.ServerStorePath))
                {
                    File.Delete(_UploadFileEntity.ServerStorePath);
                }
            }
            #endregion
        }
    }
}
